﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MishraReader.Entities;
using MishraReader.Entities.ShareServices;
using MishraReader.ViewModels.Reader;

namespace MishraReader.ViewModels.Settings
{
    

    public class ServiceAccountViewModel : ShareServiceViewModel, IServiceAccountViewModel
    {
        private readonly IShareServicePackage _servicePackage;
        private IShareServiceConfigurationViewModel _configModel;
        private string _servicePassword;
        private string _serviceToken;
        private string _serviceTokenSecret;
        private string _serviceUserName;

        public ServiceAccountViewModel(IShareServicePackage servicePackage)
            : base(servicePackage)
        {
            _servicePackage = servicePackage;

            ConfigureCommand = new RelayCommand(() =>
            {
                if (IsActivated == false)
                {
                    ConfigModel.Configure();
                }
                else
                {
                    if (IsConfigured)
                    {
                        ConfigModel.Disconnect();

                        ServiceUserName = String.Empty;
                        ServicePassword = String.Empty;
                        ServiceToken = String.Empty;
                        IsActivated = false;
                    }
                    else
                    {
                        ConfigModel.Configure();
                    }
                }
            });

            ConfigModel = _servicePackage.CreateConfigModel();
            ConfigModel.ServiceAccountViewModel = this;
        }

        public IShareServiceConfigurationViewModel ConfigModel
        {
            get { return _configModel; }
            private set
            {
                Set(ref _configModel, value);
            }
        }


        public ICommand ConfigureCommand { get; private set; }


        /// <summary>
        /// </summary>
        public bool IsConfigured
        {
            get
            {
                return
                    string.IsNullOrEmpty(ServiceToken) == false ||
                    (string.IsNullOrEmpty(ServiceUserName) == false && string.IsNullOrEmpty(ServicePassword) == false);
            }
        }

        public string ServiceName
        {
            get { return ServicePackage.ServiceName; }
        }

        public string ServicePassword
        {
            get { return _servicePassword; }
            set
            {
                if (_servicePassword != value)
                {
                    _servicePassword = value;
                    RaisePropertyChanged();
                    RaisePropertyChanged(() => IsConfigured);
                }
            }
        }

        public string ServiceToken
        {
            get { return _serviceToken; }
            set
            {
                if (_serviceToken != value)
                {
                    _serviceToken = value;
                    RaisePropertyChanged();
                    RaisePropertyChanged(() => IsConfigured);
                }
            }
        }


        public string ServiceTokenSecret
        {
            get { return _serviceTokenSecret; }
            set
            {
                Set(ref _serviceTokenSecret, value);
            }
        }

        public string ServiceUserName
        {
            get { return _serviceUserName; }
            set
            {
                if (_serviceUserName != value)
                {
                    _serviceUserName = value;
                    RaisePropertyChanged();
                    RaisePropertyChanged(() => IsConfigured);
                }
            }
        }

        public bool IsWellConfigured(string username, string password)
        {
            return ServicePackage.Service.IsWellConfigured(username, password);
        }

        /// <summary>
        ///   save changes
        /// </summary>
        /// <param name="token"> </param>
        /// <returns> </returns>
        public bool SaveChangesToken(string token)
        {
            //TODO validation feedback
            if (!string.IsNullOrEmpty(token))
            {
                ServiceToken = token;
                RaisePropertyChanged(() => IsConfigured);
                return true;
            }

            RaisePropertyChanged(() => IsConfigured);
            return false;
        }
    }
}